

#include <iostream>
#include <mutex>
#include <unistd.h>
#include "Scheduler.h"

using namespace zepco;

static unsigned int test_number;
std::mutex mutex_count;

void task() {
    {
        std::lock_guard<std::mutex> lock(mutex_count);
        std::cout << "task " << test_number ++ << " is under processing in thread: " << Thread::GetThreadID() << std::endl;
    }
    sleep(1);
}

int main() {
    {
        // 可以尝试把false 变为true 此时调度器所在线程也将加入工作线程
        std::shared_ptr<Scheduler> scheduler = std::make_shared<Scheduler>(1, false, "scheduler_1");
        scheduler->start();

        sleep(2);

        std::cout << "\nbegin post\n\n";
        for(int i = 0; i < 10; i++) {
            std::shared_ptr<Zephyr> zephry = std::make_shared<Zephyr>(task);
            scheduler->scheduleLock(zephry);
        }

        sleep(6);
#if 1
        std::cout << "\npost again\n\n";
        for(int i = 0; i < 5; i++) {
            std::shared_ptr<Zephyr> zephry = std::make_shared<Zephyr>(task);
            scheduler->scheduleLock(zephry);
        }

        sleep(3);
#endif
#if 1
        // scheduler如果有设置将加入工作处理
        scheduler->stop();
#else
#if 1
        std::shared_ptr<Zephyr> zephry = std::make_shared<Zephyr>(std::bind(&Scheduler::stop, scheduler));
        scheduler->scheduleLock(zephry);
#else
        scheduler->scheduleLock(std::bind(&Scheduler::stop, scheduler));
#endif
#endif
    }

    return 0;
}